package com.turing.system.controller;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.turing.system.service.KeeperWithdrawalsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;
import result.Result;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


/**
 * @author 26401
 */
@RestController
@RequestMapping("withdrawal")
public class KeeperWithdrawalsController {


    @Autowired
    private KeeperWithdrawalsService keeperWithdrawalsService;

    @Resource
    private RedisTemplate redisTemplate;


    private final String SESSIONS_CACHE_PREFIX = "keeper:list:";
    private final String SESSIONS_CACHE_NAME = "name:List:";

    @GetMapping("kwList")
    public Result kwList(@RequestParam(value = "merchantName", required = false) String merchantName,
                         @RequestParam(value = "pageNum", required = false) Integer pageNum,
                         @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        ListOperations ops = redisTemplate.opsForList();
        if (pageNum != null && pageSize != null) {
            if (StringUtils.isEmpty(merchantName)) {
                List<Map<String, Object>> list = ops.range(SESSIONS_CACHE_PREFIX, (pageNum - 1) * pageSize, pageNum * pageSize - 1);
                if (list.size() == 0) {
                    PageHelper.startPage(pageNum, pageSize);
                    List<LinkedHashMap<String, Object>> list1 = keeperWithdrawalsService.kwList(merchantName,pageNum,pageSize);
                    return Result.success(new PageInfo<>(list1));
                }
                return Result.success(new PageInfo<>(list));


            } else {
                PageHelper.startPage(pageNum, pageSize);
                List<LinkedHashMap<String, Object>> list = keeperWithdrawalsService.kwList(merchantName,pageNum,pageSize);
                return Result.success(new PageInfo<>(list));
            }
        } else {
            if (StringUtils.isEmpty(merchantName)) {
                List<Map<String, Object>> list = ops.range(SESSIONS_CACHE_PREFIX, 0,-1);
                if (list.size() == 0) {
                    List<LinkedHashMap<String, Object>> list1 = keeperWithdrawalsService.kwList(merchantName,null,null);
                    return Result.success(list1);
                }
                return Result.success(list);

            } else {
                List<LinkedHashMap<String, Object>> list = keeperWithdrawalsService.kwList(merchantName,null,null);
                return Result.success(list);
            }
        }


    }

}
